package com.example.myapplication.base;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.myapplication.R;
import com.example.myapplication.baseview.BaseMvpView;
import com.example.myapplication.mainactivity.presenter.Presenter;
import com.example.myapplication.model.CallBack;
import com.example.myapplication.rxjava.ApiStores;
import com.example.myapplication.utils.AppManager;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseListActivity<A,P extends Presenter<V>, V extends BaseMvpView> extends AppCompatActivity
        implements BaseMvpView, LoaderManager.LoaderCallbacks<P> , OnRefreshLoadMoreListener {

    private final int BASE_LODER_ID = 1000;//loader的id值
    protected P presenter;

    protected abstract int getResourcesId();

    private boolean isFirst = true;

    // ---------------------
    RefreshLayout refreshLayout;
    TextView empty;
    public RecyclerView list;
    public BaseQuickAdapter listAdapter;

    public ArrayList<A> data = new ArrayList<>();

    //页码
    public int pager = 1;
    //true 刷新 false  加载
    public boolean state = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getResourcesId());
        getSupportLoaderManager().initLoader(BASE_LODER_ID, null, this);//初始化loader
        AppManager.addActivity(this);
        AppManager.setCurrentActivity(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (presenter != null) {
            presenter.attachView((V) this);//presenter与view 连接
        }
        if (isFirst) {
            initView();
            isFirst = false;
        }
    }

    public void initView(){
        refreshLayout = findViewById(R.id.refreshLayout);
        empty = findViewById(R.id.empty);
        list = findViewById(R.id.list);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        list.setLayoutManager(getLayoutLayoutManager());
        listAdapter = getListAdapter();
        list.setAdapter(listAdapter);
        setEnableRefresh(true);
        autoRefresh();
    }

    public RecyclerView.LayoutManager getLayoutLayoutManager() {
        return new LinearLayoutManager(this);
    }

    protected abstract BaseQuickAdapter getListAdapter();

    protected abstract void initUrl();

    public void autoLoadMore() {
        refreshLayout.autoLoadMore();
    }



    public void setEnableLoadMore(boolean isrefresh) {
        refreshLayout.setEnableLoadMore(isrefresh);
    }

    public void setEnableRefresh(boolean isrefresh) {
        refreshLayout.setEnableRefresh(isrefresh);
    }

    public void autoRefresh() {
        state = true;
        refreshLayout.autoRefresh();
    }

    public void closeLoding() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        state = false;
        initUrl();
    }


    public CallBack<ArrayList<A>> getCallBack() {
        return new CallBack<ArrayList<A>>() {

            @Override
            public void onSuccess(ArrayList<A> ts, boolean isSuccess, String msg) {
                if (state && ts.size() == 0) {
                    empty.setVisibility(View.VISIBLE);
                    list.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.GONE);
                    list.setVisibility(View.VISIBLE);
                }
                setEnableLoadMore(ts.size() >= ApiStores.Companion.getPagerSize());
                if (state) {
                    listAdapter.setNewData(ts);
                    data = ts;
                } else {
                    listAdapter.addData(ts);
                    data.addAll(ts);
                }
                if (ts.size() >= ApiStores.Companion.getPagerSize()) {
                    pager = pager + 1;
                }
                closeLoding();
            }

            @Override
            public void onFailure(String fail) {
                Toast.makeText(BaseListActivity.this,fail,Toast.LENGTH_SHORT).show();
                closeLoding();
            }
        };
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        state = true;
        pager = 1;
        initUrl();
    }

    @Override
    public void showErr(String err) {
        Toast.makeText(this,err,Toast.LENGTH_SHORT).show();
        closeLoding();
    }

    @Override
    public void onLoadFinished(Loader<P> loader, P data) {
        presenter = data;
        presenter.attachView((V) this);
    }

    @Override
    public void onLoaderReset(Loader<P> loader) {
        presenter = null;
    }

    @Override
    protected void onDestroy() {
        presenter.detachView();//presenter与view 断开连接
        super.onDestroy();
        AppManager.reomveActivity(this);
    }
}

